#! /usr/bin/env python

"""
Use the following in /etc/munin/plugin-conf.d/wormhole :

[wormhole_*]
env.serverdir /path/to/your/wormhole/server
"""

import os, sys, time, json

CONFIG = """\
graph_title Magic-Wormhole Mailbox Events (since reboot)
graph_vlabel Events Since Reboot
graph_category network
happy.label Happy
happy.draw LINE2
happy.type GAUGE
total.label Total
total.draw LINE1
total.type GAUGE
scary.label Scary
scary.draw LINE2
scary.type GAUGE
pruney.label Pruney
pruney.draw LINE1
pruney.type GAUGE
lonely.label Lonely
lonely.draw LINE2
lonely.type GAUGE
errory.label Errory
errory.draw LINE1
errory.type GAUGE
"""

if len(sys.argv) > 1 and sys.argv[1] == "config":
    print CONFIG.rstrip()
    sys.exit(0)

serverdir = os.environ["serverdir"]
fn = os.path.join(serverdir, "stats.json")
with open(fn) as f:
    data = json.load(f)
if time.time() > data["valid_until"]:
    sys.exit(1) # expired

r = data["rendezvous"]["since_reboot"]
print "happy.value", r["mailbox_moods"].get("happy", 0)
print "total.value", r["mailboxes_total"]
print "scary.value", r["mailbox_moods"].get("scary", 0)
print "pruney.value", r["mailbox_moods"].get("pruney", 0)
print "lonely.value", r["mailbox_moods"].get("lonely", 0)
print "errory.value", r["mailbox_moods"].get("errory", 0)
